package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.service.ApArticleConfigService;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Slf4j
@Transactional
public class ApArticleConfigServiceImpl extends ServiceImpl<ApArticleConfigMapper, ApArticleConfig> implements ApArticleConfigService {
    /**
     * 修改文章的上下架状态
     *
     * @param map
     */
    @Override
    public void updateByMap(Map map) {
        //enable中  0为下架  1为上架
        Object enable = map.get("enable");
        boolean isDown=true;
        //这里如果为1的话  isDown=false=0
        if (enable.equals(1)){
            //isDown中的0为默认上架 1为默认下架
            isDown=false;
        }
        //修改文章
        LambdaUpdateWrapper<ApArticleConfig> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ApArticleConfig::getArticleId, map.get("articleId"))
                .set(ApArticleConfig::getIsDown,isDown);
        update(wrapper);
    }

    /**
     * 通过文章id查找文章配置信息
     * @param articleId
     */
    @Override
    public ResponseResult findArticleConfigByArticleId(Long articleId) {
        LambdaQueryWrapper<ApArticleConfig> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ApArticleConfig::getArticleId,articleId);
        ApArticleConfig apArticleConfig = getOne(queryWrapper);
        if (apArticleConfig==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.okResult(apArticleConfig);
    }
}
